//1658.将x减到0的最小操作数
#include<iostream>
#include<vector>
using namespace std;

int minOperations(vector<int>& nums,int x){
    //计算数组总和
    int count = 0;
    for(auto e : nums){
        count += e;
    }

    //目标值等于数组总和减去x
    int target = count - x;
    //当出现特殊情况，就是目标值等于0，说明减去整个数组，直接返回数组长度，就是最小操作数
    if(target==0){
        return nums.size();
    }
    int sum = 0;

    int left = 0, right = 0;
    int maxlen = 0;

    while(right<nums.size()){
        //进窗口
        sum += nums[right];

        //当子数组和大于目标值时，出窗口
        if(sum>target){
            while(left<right&&sum>target){
                sum -= nums[left++];
            }
        }
        //当等于目标值时，就是要找的子数组，计算子数组长度并更新sum值
        if(sum==target){
            maxlen = max(maxlen, right - left + 1);
            sum -= nums[left++];
        }
        right++;
    }

    //如果最大数组长度不为0，最小操作数就是数组长度减去最大数组长度
    int minoperations=-1;
    if(maxlen){
        minoperations = nums.size() - maxlen;
    }

    return minoperations;
}

int main(){
    int x=134365;
    vector<int> nums = {8828,9581,49,9818,9974,9869,9991,10000,10000,10000,9999,9993,9904,8819,1231,6309};

    int min = minOperations(nums, x);
    cout << min;
    return 0;
}